package model

import (
	"fmt"
	errorStatus "gin_note/error_status"
)

type user struct {
	Username string
	Password string
}

//用户登录
func User_login(username string, password string) int {
	sqlStr := "select username,password from user where username=? and password = ?"
	var u user
	err := DB.Get(&u, sqlStr, username, password)
	if err != nil {
		fmt.Printf("get failed, err:%v\n", err)
		return errorStatus.StatusLogin
	}
	return 0
}

//用户修改密码
func User_password(username, oldpassword, newpassword string) int {
	sqlStr := "update user set password = ? where username=? and password = ?"
	_, err := DB.Exec(sqlStr, newpassword, username, oldpassword)
	if err != nil {
		fmt.Printf("get failed, err:%v\n", err)
		return errorStatus.StatusPassword
	}
	return 0
}

//用户注册
func User_zhuce(username string, password string) int {
	sqlStr := "insert into user (username,password) values (?,?)"
	_, err := DB.Exec(sqlStr, username, password)
	if err != nil {
		fmt.Printf("get failed, err:%v\n", err)
		return errorStatus.StatusPassword
	}
	return 0
}
